<?php


/**
 *
 *    添加开发模块管理
 *
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'login';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/* 初始化 $exc 对象 */
$exc = new exchange($ecs->table("admin_action"), $db, 'action_id ', 'action_name');

/*------------------------------------------------------ */
//-- 模块列表页面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	//权限的语言包
    include_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/priv_action.php');
	
	$role_action_array = role_action_list(0, 0, false);

	$smarty->assign('ur_here',     '模块行为列表');
    $smarty->assign('action_link', array('text' => '添加模块', 'href' => 'role_action.php?act=add'));
    $smarty->assign('full_page',   1);
    $smarty->assign('role_action_array',        $role_action_array);
    assign_query_info();
    $smarty->display('role_action_list.htm');
}

/*------------------------------------------------------ */
//-- 添加模块
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
   // admin_priv('article_cat');

	/* 得到一级模块 */
    $sql_query = "SELECT action_id, parent_id, action_code, action_name,relevance FROM " .$ecs->table('admin_action').
                 " WHERE parent_id = 0";
    $res = $db->query($sql_query);
    while ($rows = $db->FetchRow($res))
    {
        $priv_arr[$rows['action_id']] = $rows;
    }

    $smarty->assign('action_select',  $priv_arr);
    $smarty->assign('ur_here',     '添加模块');
    $smarty->assign('action_link', array('text' => '模块列表', 'href' => 'role_action.php?act=list'));
	$smarty->assign('button_value','添加');
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('role_action_info.htm');
}


/*------------------------------------------------------ */
//-- 执行插入
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    //admin_priv('article_cat');

    /*检查模块名是否重复*/
 

    $sql = "INSERT INTO ".$ecs->table('admin_action')."(action_name, action_code,  action_url, flag,parent_id)
           VALUES ('$_POST[action_name]', '$_POST[action_code]','$_POST[action_url]','$_POST[flag]', '$_POST[parent_id]')";
    $db->query($sql);


    admin_log($_POST['action_name'],'add','role_action');

    $link[0]['text'] = '继续添加模块';
    $link[0]['href'] = 'role_action.php?act=add';

    $link[1]['text'] = '返回模块列表';
    $link[1]['href'] = 'role_action.php?act=list';
    clear_cache_files();
    sys_msg($_POST['action_name'].'添加成功',0, $link);
}

/*------------------------------------------------------ */
//-- 编辑模块
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
   // admin_priv('article_cat');

	/* 得到一级模块 */
    $sql_query = "SELECT action_id, parent_id, action_code, action_name,relevance FROM " .$ecs->table('admin_action').
                 " WHERE parent_id = 0";
    $res = $db->query($sql_query);
    while ($rows = $db->FetchRow($res))
    {
        $priv_arr[$rows['action_id']] = $rows;
    }
	
	/*得到要编辑的模块*/
	$sql = "SELECT * FROM ".$ecs->table('admin_action'). " WHERE action_id='$_REQUEST[id]'";
    $action = $db->GetRow($sql);
	

	$smarty->assign('action',  $action);
    $smarty->assign('action_select',  $priv_arr);
    $smarty->assign('ur_here',     '编辑模块');
    $smarty->assign('action_link', array('text' => '返回模块列表', 'href' => 'role_action.php?act=list'));
	$smarty->assign('button_value','编辑');
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('role_action_info.htm');
}

/*------------------------------------------------------ */
//-- 更新模块信息
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'update')
{
	
    /* 更新管理员的权限 */
    $sql = "UPDATE " .$ecs->table('admin_action'). " SET action_url='".$_POST['action_url']."',flag = '".$_POST['flag']."',action_name = '".$_POST['action_name']."', action_code = '".$_POST['action_code']."', parent_id = '".$_POST['parent_id']." ' ".
           "WHERE action_id = '$_POST[id]'";
    $db->query($sql);

    $link[0]['text'] = '继续添加模块';
    $link[0]['href'] = 'role_action.php?act=add';

    $link[1]['text'] = '返回模块列表';
    $link[1]['href'] = 'role_action.php?act=list';
	 sys_msg($_POST['action_name'].'编辑成功',0, $link);
}

/*------------------------------------------------------ */
//-- 查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
   
	$role_action_array = role_action_list(0, 0, false);

    $smarty->assign('role_action_array',        $role_action_array);


    make_json_result($smarty->fetch('role_action_list.htm'));
}


/*------------------------------------------------------ */
//-- 删除一个模块
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
   // check_authz_json('admin_drop');

    $id = intval($_GET['id']);
    $num_sql = "SELECT count(*) FROM " .$ecs->table('admin_action'). " WHERE parent_id = '$_GET[id]'";
    $remove_num = $db->getOne($num_sql);
    if($remove_num > 0)
    {
        make_json_error('请先删除子分类');
    }
    else
    {
        $exc->drop($id);
		 clear_cache_files();
       $url = 'role_action.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    }

    ecs_header("Location: $url\n");
    exit;
}




?>
